package com.yxhl.zoume.core.user.presenter.passenger;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.view.passenger.AddPassengerView;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.data.http.rest.param.passenger.AddPassengerParam;
import com.yxhl.zoume.data.http.rest.param.passenger.PassengerParam;
import com.yxhl.zoume.data.http.rest.response.passenger.AddPassengerResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.passenger.AddPassengerUseCase;
import com.yxhl.zoume.utils.LOG;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/3/26.
 */
public class AddPassengerPresenter extends BasePresenter implements IPresenter {

    private static final String TAG = "AddPassengerPresenter";

    private AddPassengerUseCase mAddPassengerUseCase;
    private AddPassengerView mAddPassengerView;

    @Inject
    public AddPassengerPresenter(AddPassengerUseCase mAddPassengerUseCase, Context context) {
        super(context);
        this.mAddPassengerUseCase = mAddPassengerUseCase;
    }

    /**
     * 确认新增乘客 网络请求回调
     *
     * @param passengerParam
     */
    public void confirmAddPassenger(PassengerParam passengerParam) {
        final AddPassengerParam addPassengerParam = new AddPassengerParam(
                getMobile(),
                getToken(),
                passengerParam
        );
        mAddPassengerUseCase.execute(addPassengerParam,new ZMSubscriber<AddPassengerResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mAddPassengerView.renderDataOnError();
            }

            @Override
            public void onNext(AddPassengerResponse addPassengerResponse) {
                super.onNext(addPassengerResponse);
                if (addPassengerResponse != null) {
                    boolean succ = addPassengerResponse.isSucc();
                    if (succ) {
                        ZMPassenger passenger = addPassengerResponse.getPassenger();
                        if (passenger != null) {
                            LOG.e(TAG, "passenger=" + passenger);
                            mAddPassengerView.renderDataOnSuccess(passenger);
                        }
                    } else {
                        String resultMessage = addPassengerResponse.getResultMessage();
                        mAddPassengerView.renderDataOnFailure(resultMessage);
                    }
                }
            }
        });
    }


    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        mAddPassengerUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view != null) {
            if (view instanceof AddPassengerView) {
                mAddPassengerView = (AddPassengerView) view;
            }
        }
    }
}
